#!/bin/bash
set -e

while getopts  ":s:t:h" opts
do
	case  $opts  in
	s)
		sample_name=$OPTARG
		;;
	t)
		type=$OPTARG
	;;
	h)
		echo `basename $0` "[-s sample_name] [-t snp|cnv] file"
		exit 1
		;;
	\?)
		
		echo `basename $0`': invalid option'
		exit 1
		;;
	esac
done
shift $(($OPTIND - 1))

if [ -z "$1" ]; then
	echo `basename $0` "[-s sample_name] [-t snp|cnv] file"
	exit 1
fi

. /mnt/ilustre/app/medical/tools/.var

# fil_name=palm.xy.zeiss.txt

# perl \r return like \r\n
# regex ^$ expect empty line

dir=`date +%m%d_%I%M%p_%y` 
mkdir $dir && cp $1 $dir && cd $dir

for item in $(cat $1|grep -v '^$'|grep -vP '^\r'); do
name=`echo $item |cut -d',' -f1`
echo $item |cut -d',' -f2- > xy.txt

Rscript ${tools_path}/script/pdf_palm.r --args \
xy.txt \
$name


sed -i 1s/^/"$name\t"/ $name.xys.txt
convert -density 300 $name.xys.txt $name.xys.pdf
cpdf -o 1.$name.xys.pdf 1.$name.pdf $name.xys.pdf

done

cpdf -merge -o $1.$dir.pdf 1.*.xys.pdf

dest_sub=tes/xy

bk.sh -t$dest_sub $1.$dir.pdf

mail.sh xy no $dest_sub/$dir/$1.$dir.pdf $1

# cat $1|grep -vP '^\r'|cut -d',' -f1 > names.txt
# cat $1|grep -vP '^\r'|cut -d',' -f2- > xy.txt

# Rscript ${tools_path}/script/pdf_palm.r --args \
# xy.txt \
# names.txt

